package com.example.dailyrecord.base.controller;

import com.example.dailyrecord.base.entity.ResultMsg;
import com.example.dailyrecord.base.service.BaseService;
import com.example.dailyrecord.entity.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseController<T> {
    @Autowired
    protected BaseService<T> baseService;

    @DeleteMapping("/deleteRequest")
    @ApiOperation(value = "删除", notes = "删除")
    public ResultMsg deleteRequest(HttpServletRequest request, HttpServletResponse response) {
        baseService.deleteRequest(request, response);
        return ResultMsg.success();
    }

    @PostMapping("/updateRequest")
    @ApiOperation(value = "更新", notes = "更新")
    public ResultMsg updateRequest(HttpServletRequest request, HttpServletResponse response) {
        T t = baseService.updateRequest(request, response);
        return ResultMsg.getInstance(t, StatusCode.SUCCESS);
    }

    @GetMapping("/getOneRequest")
    @ApiOperation(value = "查询", notes = "查询")
    public ResultMsg selectRequest(HttpServletRequest request, HttpServletResponse response) {
        return ResultMsg.getInstance(baseService.selectRequest(request, response), StatusCode.SUCCESS);
    }

    @PostMapping("/insertRequest")
    @ApiOperation(value = "插入", notes = "插入")
    public ResultMsg insertRequest(HttpServletRequest request, HttpServletResponse response) {
        return ResultMsg.getInstance(baseService.insertRequest(request, response));
    }

    @GetMapping("/getListRequest")
    @ApiOperation(value = "查询列表", notes = "查询列表")
    public ResultMsg getListRequest(HttpServletRequest request, HttpServletResponse response) {
        return ResultMsg.getInstance(baseService.getListRequest(request, response), StatusCode.SUCCESS);
    }

    @GetMapping("/countRequest")
    @ApiOperation(value = "查询数量", notes = "查询数量")
    public ResultMsg countRequest(HttpServletRequest request, HttpServletResponse response) {
        return ResultMsg.getInstance(baseService.countRequest(request, response), StatusCode.SUCCESS);
    }
}
